<?php
namespace Admin\Model;
use Think\Model\RelationModel;

class HomeworkModel extends RelationModel {

	protected $_validate = array(
		array('id', 'require', 'id不能为空！'),
		array('title', 'require', '名称不能为空！'),
		array('content', 'require', '内容不能为空！'),
		array('start_time', 'require', '开始时间不能为空！', 1),
		array('end_time', 'require', '截止时间不能为空！', 1),
		array('logic_class_id', 'require', '逻辑班级不能为空！'),
		array('logic_class_id', 'number', '逻辑班级值有误！'),
	);

	protected $_auto = array(
		array('start_time', 'trim', 3, 'function'),
		array('start_time', 'strtotime', 3, 'function'),
		array('end_time', 'trim', 3, 'function'),
		array('end_time', 'strtotime', 3, 'function'),
	);

	protected $_link = array(
		'logic_class' => array(
			'mapping_type'	=> self::BELONGS_TO,
			'class_name'	=> 'LogicClass',
			'foreign_key'	=> 'logic_class_id',
		),
	);

	public function checkUnique($homework) {
		$map['title'] = $homework['title'];
		$map['logic_class_id'] = $homework['logic_class_id'];
		$result = $this->where($map)->find();
		if(isset($homework['id']) && $result && $result['id'] == $homework['id']) {
			return true;
		}
		return !$result;
	}

}
